package com.example.demo.controller;

import com.example.demo.model.Student;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

// 给类加注解，方便查找代码
// 类注解和方法注解一起使用
@RequestMapping("/request")
@RestController
public class RequestController {
    // 练习使用网页打印 hello world
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String sayHello(){
        return "Hello, spring mvc";
    }
    // 练习接收 url 中的参数
    @RequestMapping("/r1")
    public String r1(String name){
        return "接收到参数，name:" + name;
    }
    @RequestMapping("/r2")
    public String r2(Integer age){
        return "接收到参数，age:"+ age;
    }
    @RequestMapping("/r3")
    public String r3(int age){
        return "接收到参数，age:"+ age;
    }
    @RequestMapping("/r4")
    public String r4(String name, Integer age){
        return "接收到参数，name=" + name + ", age=" + age;
    }
    @RequestMapping("/r5")
    public String r5(Student student){
        return "接收到参数，Student=" + student;
    }
    @RequestMapping("/r6")
    public String r6(@RequestParam("name") String username){
        return "接收到参数，name:" + username;
    }
    @RequestMapping("/r7")
    public String r7(@RequestParam(value = "name", required = false) String username){
        return "接收到参数，name:" + username;
    }
    /**
     * 接收集合
     * @param array
     * @return
     */
    @RequestMapping("/r8")
    public String r8(String[] array){
        return "接收到参数，array:" + Arrays.toString(array);
    }
    /**
     * 接收集合
     * @param list
     * @return
     */
    @RequestMapping("/r9")
    public String r9(@RequestParam(required = false) List<String> list){
        return "接收到参数，list:" + list;
    }

    /**
     * 接收 json
     * @param student
     * @return
     */
    @RequestMapping("/r10")
    public String r10(@RequestBody Student student){
        return "接收到参数，student:" + student;
    }
    /**
     * 从路径中接收参数
     * @return
     */
    @RequestMapping("/article/{articleId}")
    public String r11(@PathVariable("articleId") Integer articleId){
        return "接收到参数，articleId:" + articleId;
    }

    /**
     * 从路径中接收参数
     * @param articleId
     * @param name
     * @return
     */
    @RequestMapping("/article/{articleId}/{name}")
    public String r11(@PathVariable("articleId") Integer articleId, @PathVariable("name") String name){
        return "接收到参数，articleId:" + articleId + ", name:" + name;
    }

    /**
     * 上传文件
     * @param imageFile
     * @return
     */
    @RequestMapping("/r13")
    public String r13(@RequestPart("file") MultipartFile imageFile){
        String originalFilename = imageFile.getOriginalFilename();
        return "接收到文件，文件名称：" + originalFilename;
    }
}
